package dao.impl;

import dao.XsbDao;
import entity.XsbEntity;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author Lei
 * @create 2021-04-16 9:48
 */
@Repository
public class XsbDaoImpl implements XsbDao {

    @Resource
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }


    @Override
    public List<XsbEntity> selectAllRecord(String xh, String xm) {
        List<XsbEntity> xsbEntities = (List<XsbEntity>) this.hibernateTemplate.find("from XsbEntity where xh like ? and xm like ?", "%" + xh + "%", "%" + xm + "%");
        return xsbEntities;
    }

    @Override
    @Transactional
    public int addUser(XsbEntity xsbEntity) {
        Serializable save = this.hibernateTemplate.save(xsbEntity);
        return 1;
    }

    @Override
    @Transactional
    public int updateUser(XsbEntity xsbEntity) {
        this.hibernateTemplate.update(xsbEntity);
        return 1;
    }

    @Override
    @Transactional
    public int deleteUser(XsbEntity xsbEntity) {
        this.hibernateTemplate.delete(xsbEntity);
        return 1;
    }

    @Override
    public XsbEntity selectRecord(String xh) {
        XsbEntity xsbEntity = new XsbEntity();
        xsbEntity.setXh(xh);
        List<XsbEntity> objects = (List<XsbEntity>) this.hibernateTemplate.find("from XsbEntity where xh = ?", xh);
        if (objects.size() != 0) {
            return objects.get(objects.size() - 1);
        } else {
            return null;
        }
    }
}
